EC-CUBE 2.11.4
[ class tree: EC-CUBE 2.11.4 ] [ index: EC-CUBE 2.11.4 ] [ all elements ]

Source for file SC_SelectSql.php

Documentation is available at SC_SelectSql.php

  1. <?php
  2. /*
  3.  * This file is part of EC-CUBE
  4.  *
  5.  * Copyright(c) 2000-2011 LOCKON CO.,LTD. All Rights Reserved.
  6.  *
  7.  * http://www.lockon.co.jp/
  8.  *
  9.  * This program is free software; you can redistribute it and/or
  10.  * modify it under the terms of the GNU General Public License
  11.  * as published by the Free Software Foundation; either version 2
  12.  * of the License, or (at your option) any later version.
  13.  *
  14.  * This program is distributed in the hope that it will be useful,
  15.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  17.  * GNU General Public License for more details.
  18.  *
  19.  * You should have received a copy of the GNU General Public License
  20.  * along with this program; if not, write to the Free Software
  21.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  22.  */
  23.  
  24. /* ---- SQL文を作るクラス ---- */
  25. class SC_SelectSql {
  26.  
  27.     var $sql;
  28.  
  29.     var $select;
  30.     var $where;
  31.     var $order;
  32.     var $group;
  33.     var $limit;
  34.     var $offset;
  35.     var $arrSql;
  36.     var $arrVal;
  37.  
  38.     //-- コンストラクタ。
  39.     function SC_SelectSql($array = "") {
  40.         if (is_array($array)) {
  41.             $this->arrSql = $array;
  42.         }
  43.     }
  44.  
  45.     //-- SQL分生成
  46.     function getSql( $mode = "" ){
  47.         $this->sql = $this->select ." ". $this->where ." ". $this->group ." ";
  48.  
  49.         // $mode == 1 は limit & offset無し
  50.         if ($mode == 2) {
  51.             $this->sql .= $this->order;
  52.         }elseif ( $mode != 1 ){
  53.             $this->sql .= $this->order . " " .$this->limit ." ". $this->offset;
  54.         }
  55.  
  56.         return $this->sql;
  57.     }
  58.  
  59.         // 検索用
  60.     function addSearchStr($val) {
  61.         $return = "%" .$val. "%";
  62.         return $return;
  63.     }
  64.  
  65.     //-- 範囲検索(○ ~ ○ まで)
  66.     function selectRange($from, $to, $column) {
  67.  
  68.         // ある単位のみ検索($from = $to)
  69.         if(  $from == $to ) {
  70.             $this->setWhere( $column ." = ?" );
  71.             $return = array($from);
  72.         // ~$toまで検索
  73.         } elseif(  strlen($from) == 0 && strlen($to) > 0 ) {
  74.             $this->setWhere( $column ." <= ? ");
  75.             $return = array($to);
  76.         // ~$from以上を検索
  77.         } elseif(  strlen($from) > 0 && strlen($to) == 0 ) {
  78.             $this->setWhere( $column ." >= ? ");
  79.             $return = array($from);
  80.         // $from~$toの検索
  81.         } else {
  82.             $this->setWhere( $column ." BETWEEN ? AND ?" );
  83.             $return = array($from, $to);
  84.         }
  85.         return $return;
  86.     }
  87.  
  88.     //-- 期間検索(○年○月○日か~○年○月○日まで)
  89.     function selectTermRange($from_year, $from_month, $from_day, $to_year, $to_month, $to_day, $column) {
  90.         $return = array();
  91.  
  92.         // 開始期間の構築
  93.         $date1 = $from_year . "/" . $from_month . "/" . $from_day;
  94.  
  95.         // 終了期間の構築
  96.         // @see http://svn.ec-cube.net/open_trac/ticket/328
  97.         // FIXME とりあえずintvalで対策...
  98.         $date2 = mktime (0, 0, 0, intval($to_month), intval($to_day), intval($to_year));
  99.         $date2 = $date2 + 86400;
  100.         // SQL文のdate関数に与えるフォーマットは、yyyy/mm/ddで指定する。
  101.         $date2 = date('Y/m/d', $date2);
  102.  
  103.         // 開始期間だけ指定の場合
  104.         if( ( $from_year != "" ) && ( $from_month != "" ) && ( $from_day != "" ) && ( $to_year == "" ) && ( $to_month == "" ) && ( $to_day == "" ) ) {
  105.             $this->setWhere( $column .' >= ?');
  106.             $return[] = $date1;
  107.         }
  108.  
  109.         // 開始~終了
  110.         if( ( $from_year != "" ) && ( $from_month != "" ) && ( $from_day != "" ) &&
  111.             ( $to_year != "" ) && ( $to_month != "" ) && ( $to_day != "" ) ) {
  112.             $this->setWhere( $column . ' >= ? AND ' . $column . ' < date(?)' );
  113.             $return[] = $date1;
  114.             $return[] = $date2;
  115.         }
  116.  
  117.         // 終了期間だけ指定の場合
  118.         if( ( $from_year == "" ) && ( $from_month == "" ) && ( $from_day == "" ) && ( $to_year != "" ) && ( $to_month != "" ) && ( $to_day != "" ) ) {
  119.             $this->setWhere( $column . ' < date(?)');
  120.             $return[] = $date2;
  121.         }
  122.  
  123.         return $return;
  124.     }
  125.  
  126.     // checkboxなどで同一カラム内で単一、もしくは複数選択肢が有る場合 例: AND ( sex = xxx OR sex = xxx OR sex = xxx  ) AND ...
  127.     function setItemTerm( $arr, $ItemStr ) {
  128.  
  129.         foreach( $arr as $data ) {
  130.  
  131.             if( count( $arr ) > 1 ) {
  132.                 if( ! is_null( $data ) ) $item .= $ItemStr . " = ? OR ";
  133.             } else {
  134.                 if( ! is_null( $data ) ) $item = $ItemStr . " = ?";
  135.             }
  136.             $return[] = $data;
  137.         }
  138.  
  139.         if( count( $arr ) > 1 )  $item = "( " . rtrim( $item, " OR " ) . " )";
  140.         $this->setWhere( $item );
  141.         return $return;
  142.     }
  143.  
  144.     // NULL値が必要な場合
  145.     function setItemTermWithNull( $arr, $ItemStr ) {
  146.  
  147.         $item = " ${ItemStr} IS NULL ";
  148.  
  149.         if ( $arr ){
  150.             foreach( $arr as $data ) {
  151.                 if ($data != "不明") {
  152.                     $item .= " OR ${ItemStr} = ?";
  153.                     $return[] = $data;
  154.                 }
  155.             }
  156.         }
  157.  
  158.         $item = "( ${item} ) ";
  159.         $this->setWhere( $item );
  160.         return $return;
  161.     }
  162.     // NULLもしくは''で検索する場合
  163.     function setItemTermWithNullAndSpace( $arr, $ItemStr ) {
  164.         $count = count($arr);
  165.         $item = " ${ItemStr} IS NULL OR ${ItemStr} = '' ";
  166.         $i = 1;
  167.         if ( $arr ){
  168.             foreach( $arr as $data ) {
  169.                 if ($i == $count) break;
  170.                 $item .= " OR ${ItemStr} = ?";
  171.                 $return[] = $data;
  172.                 $i ++;
  173.             }
  174.         }
  175.         $item = "( ${item} ) ";
  176.         $this->setWhere( $item );
  177.         return $return;
  178.     }
  179.  
  180.     /* 複数のカラムでORで優先検索する場合 例: AND ( item_flag1 = xxx OR item_flag2 = xxx OR item_flag3 = xxx  ) AND ...
  181.  
  182.         配列の構造例 
  183.         if ( $_POST['show_site1'] ) $arrShowsite_1 = array( 'column' => "show_site1",
  184.                                                             'value'  => $_POST['show_site1'] );
  185.  
  186.     */
  187.     function setWhereByOR( $arrWhere ){
  188.  
  189.         $count = count( $arrWhere );
  190.  
  191.         for( $i = 0; $i < $count; $i++ ) {
  192.  
  193.             if( isset( $arrWhere[$i]['value'] ) ) $statement .= $arrWhere[$i]['column'] ." = " . SC_Utils_Ex::sfQuoteSmart($arrWhere[$i]['value']) ." OR "  ;
  194.         }
  195.  
  196.         $statement = "( " . rtrim( $statement, " OR " ) . " )";
  197.  
  198.         if( $this->where ) {
  199.  
  200.             $this->where .= " AND " . $statement;
  201.  
  202.         } else {
  203.  
  204.             $this->where = "WHERE " . $statement;
  205.         }
  206.     }
  207.  
  208.     function setWhere($where){
  209.         if ($where != "") {
  210.             if( $this->where ) {
  211.  
  212.                 $this->where .= " AND " . $where;
  213.  
  214.             } else {
  215.  
  216.                 $this->where = "WHERE " . $where;
  217.             }
  218.         }
  219.     }
  220.  
  221.     function setOrder($order){
  222.  
  223.             $this->order =  "ORDER BY " . $order;
  224.  
  225.     }
  226.  
  227.     function setGroup( $group ) {
  228.  
  229.         $this->group =  "GROUP BY " . $group;
  230.  
  231.     }
  232.  
  233.     function setLimitOffset( $limit, $offset ){
  234.  
  235.         if ( is_numeric($limit) and is_numeric($offset) ){
  236.  
  237.             $this->limit = " LIMIT " .$limit;
  238.             $this->offset = " OFFSET " .$offset;
  239.         }
  240.     }
  241.  
  242.     function clearSql(){
  243.         $this->select = "";
  244.         $this->where = "";
  245.         $this->group = "";
  246.         $this->order = "";
  247.         $this->limit = "";
  248.         $this->offset = "";
  249.     }
  250.  
  251.     function setSelect($sql) {
  252.         $this->select = $sql;
  253.     }
  254. }
  255. ?>

Documentation generated on Fri, 24 Feb 2012 14:02:55 +0900 by Seasoft